package com.mxpio.erp.common.common.provider;

import com.mxpioframework.jpa.query.Criterion;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.SimpleCriterion;
import com.mxpioframework.security.access.datascope.provider.DataScapeProvider;
import com.mxpioframework.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author : wpp
 * @Datetime : 2023/4/19 14:25
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc : 通用 当前用户等于创建人
 */
@Component
public class CreateByProvider implements DataScapeProvider {
    @Override
    public List<Criterion> provide() {
        String loginUsername = SecurityUtils.getLoginUsername();
        List<Criterion> appendCriterions = new ArrayList<>();
        if (StringUtils.isBlank(loginUsername)){
            appendCriterions.add(new SimpleCriterion("createTime", Operator.EQ,new Date(0)));//如果未匹配到过滤条件，则增加一个不能正常匹配到数据的条件
            return appendCriterions;
        }

        appendCriterions.add(new SimpleCriterion("createBy", Operator.EQ, loginUsername));//确保使用该过滤器的业务表，有bizMan的字段
        return appendCriterions;
    }
}
